import { PLAY_MODE } from "@/assets/js/constant";

export function setSelectPlay({commit},{list,index}){
    commit('setPlayMode',PLAY_MODE.sequence)
    commit('setSequenceList',list)
    commit('setPlayingState',true)
    commit('setFullScreen',true)
    commit('setPlayList',list)
    commit('setCurrentIndex',index)
}

export function randomPlay({commit},list){
    commit('setPlayMode',PLAY_MODE.random)
    commit('setSequenceList',list)
    commit('setPlayingState',true)
    commit('setFullScreen',true)
    commit('setPlayList',shuffle(list))
    commit('setCurrentIndex',0)
}

//随机播放函数
function shuffle(source){
    const arr = source.slice()
    for (let i = 0; i < arr.length; i++){
        const j = getRandomInt(i)
        swap(arr, i, j)
    }
    return arr

}

function getRandomInt(max){
    return Math.floor(Math.random() * (max + 1 ))
}

function swap(arr, i ,j){
    const t = arr[i]
    arr[i] = arr[j]
    arr[j] = t
}


